﻿using System;
using System.Threading;
using System.Web;
using BikeBouncer.BusinessLayer;
using BikeBouncer.BusinessLayer.Entities;

namespace BikeBouncer.Site
{
	public class SessionManager
	{
		#region Address session

		public static String CurrentCity
		{
			get
			{
				Object s = HttpContext.Current.Session["CurrentCity"];

				if (s == null)
				{
					return CookieManager.CurrentCity;
				}

				return s.ToString();
			}
			set
			{
				HttpContext.Current.Session["CurrentCity"] = value;
			}
		}

		public static String CurrentCountry
		{
			get
			{
				Object s = HttpContext.Current.Session["CurrentCountry"];

				if (s == null)
				{
					return CookieManager.CurrentCountry;
				}

				return s.ToString();
			}
			set
			{
				HttpContext.Current.Session["CurrentCountry"] = value;
			}
		}

		#endregion

		#region User session

		public static User LoggedUser
		{
			get
			{
				Object s = HttpContext.Current.Session["LoggedUser"];

				if (s != null)
				{
					return (User)s;
				}

				User c = CookieManager.LoggedUser;

				if (c != null)
				{
					LoggedUser = c;
				}

				return c;
			}
			set
			{
				HttpContext.Current.Session["LoggedUser"] = value;

				if (value != null)
				{
					ThreadPool.QueueUserWorkItem(DataAccess.User_UpdateLoginDateAsync, value);
				}
			}
		}

		#endregion

		#region Item session

		public static Item CurrentItem
		{
			get
			{
				Object s = HttpContext.Current.Session["CurrentItem"];

				if (s != null)
				{
					return (Item)s;
				}

				return null;
			}
			set
			{
				HttpContext.Current.Session["CurrentItem"] = value;
			}
		}

		#endregion

		#region Prevent attacks

		public static Int32 LoginAttempts
		{
			get
			{
				Object s = HttpContext.Current.Session["LoginAttempts"];

				if (s != null)
				{
					return (Int32)s;
				}

				return 0;
			}
			set
			{
				if (value > 0)
				{
					HttpContext.Current.Session["LoginAttempts"] = value;
				}
				else
				{
					HttpContext.Current.Session["LoginAttempts"] = null;
				}
			}
		}

		public static Int32 SearchCount
		{
			get
			{
				Object s = HttpContext.Current.Session["SearchCount"];

				if (s != null)
				{
					return (Int32)s;
				}

				return 0;
			}
			set
			{
				if (value > 0)
				{
					HttpContext.Current.Session["SearchCount"] = value;
				}
				else
				{
					HttpContext.Current.Session["SearchCount"] = null;
				}
			}
		}

		public static DateTime SearchCountDate
		{
			get
			{
				Object s = HttpContext.Current.Session["SearchCountDate"];

				if (s != null)
				{
					return (DateTime)s;
				}

				return DateTime.MinValue;
			}
			set
			{
				HttpContext.Current.Session["SearchCountDate"] = value;
			}
		}

		#endregion

		#region Helper methods

		public static void NeedsUserLogin()
		{
			if (LoggedUser == null)
			{
				HttpContext.Current.Response.Redirect("/Login.aspx?r=" + HttpContext.Current.Request.Path);
			}
		}

		#endregion
	}
}